package com.aote.rs.db;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.QueryParam;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;

public class DataBase {

	public DataBase() {
	}

	private HibernateTemplate hibernateTemplate;

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	@Transactional
	@POST
	public void save(@QueryParam("xpath")
	String xpath, String in) {
		xpath = "com.aote.table." + xpath.substring(1);
		Class c = null;
		try {
			c = Class.forName(xpath);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		}
		JSONObject jsonPerson = JSONObject.fromObject(decodeUri(in)
				.toLowerCase());
		filter(jsonPerson);
		Object msg = JSONObject.toBean(jsonPerson, c);
		hibernateTemplate.save(msg);
	}

	@Transactional
	@GET
	public String load(@QueryParam("xpath")
	String xpath, @QueryParam("action")
	String action) {
		// get
		String hql = getWhereConditionForHQL(decodeUri(xpath));
		List list = hibernateTemplate.find(hql);
		String jsons = JSONArray.fromObject(list).toString();
		// update
		String update = getUpdateHQL(decodeUri(xpath));
		hibernateTemplate.bulkUpdate(update);
		return jsons;
	}

	/*
	 * 解析hql
	 */
	private String getWhereConditionForHQL(String xpath) {
		String hql = " from ";
		if(xpath.indexOf("[")!=-1){
			String clas = xpath.substring(1, xpath.indexOf("["));
			String condition = " where "
					+ xpath.substring(xpath.indexOf("[") + 1, xpath.indexOf("]"));
			return hql + clas + condition.toLowerCase();
		}
		return hql+xpath.substring(1);
		
	}

	private String getUpdateHQL(String xpath) {
		String hql = " update ";
		String clas = xpath.substring(1, xpath.indexOf("["));
		String action = xpath.substring(xpath.indexOf("(") + 1, xpath
				.indexOf(")"));
		String condition = " where "
				+ xpath.substring(xpath.indexOf("[") + 1, xpath.indexOf("]"));
		return hql + clas + " set " + action + condition.toLowerCase();
	}

	/*
	 * 
	 */
	private String decodeUri(String uri) {
		try {
			uri = URLDecoder.decode(uri, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return uri;
	}

	/*
	 * 过滤json不需要存储的信息
	 */
	private void filter(JSONObject json) {
		Iterator ite = json.keySet().iterator();
		while (ite.hasNext()) {
			String key = ite.next().toString();
			// Expression(json, key);
			if (key.equals("dispatcher") || key.equals("xpath")
					|| key.equals("next") || key.equals("uri")
					|| key.equals("list")) {
				ite.remove();
			}
		}
	}

	/*
	 * 获得当前时间
	 */
	private static String dateToStrLong(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}
}
